package statistics.demo.statistics_demo;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;

public class SleepActivity extends BaseActivity {

    private ImageView mBackBtn;
    private ImageView mShareBtn;
    private BarChart mBarChart;
    private String[] mHours = new String[] {
            "22",
            "23",
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6"
    };

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_sleep);
        mBackBtn = getViewById(R.id.sleepBack);
        mShareBtn = getViewById(R.id.sleepShare);
        mBarChart = getViewById(R.id.sleepChart);
    }

    @Override
    protected void setListener() {
        mBackBtn.setOnClickListener(this);
        mShareBtn.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        /**
         * 图表数据绑定
         * 具体设置可参考 https://github.com/PhilJay/MPAndroidChart
         */
        mBarChart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the mBarChart, no values will be
        // drawn
        mBarChart.setMaxVisibleValueCount(20);

        // scaling can now only be done on x- and y-axis separately
        mBarChart.setPinchZoom(false);

        mBarChart.setDrawBarShadow(false);
        mBarChart.setDrawGridBackground(false);
        mBarChart.setDrawBorders(false);
        mBarChart.setBorderWidth(0f);


        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.TOP);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setGranularity(1f);
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum((float) (mHours.length - 0.6));
        xAxis.setLabelCount(mHours.length, true);

        xAxis.setValueFormatter(new IndexAxisValueFormatter(mHours));


        mBarChart.getAxisRight().setDrawLabels(false);
        mBarChart.getAxisLeft().setDrawLabels(false);


        mBarChart.getAxisLeft().setDrawGridLines(false);
        mBarChart.getAxisRight().setDrawGridLines(false);
        mBarChart.getAxisLeft().setDrawAxisLine(false);
        mBarChart.getAxisRight().setDrawAxisLine(false);

        // add a nice and smooth animation
        mBarChart.animateY(1500);

        mBarChart.getLegend().setEnabled(false);

        ArrayList<BarEntry> values1 = new ArrayList<>();
        ArrayList<BarEntry> values2 = new ArrayList<>();
        ArrayList<BarEntry> values3 = new ArrayList<>();
        ArrayList<BarEntry> values4 = new ArrayList<>();

        for (int i = 0; i < mHours.length; i++) {
            float multi = (mHours.length + 1);
            float val1 = (float) (Math.random() * multi) + multi / 3;
            float val2 = (float) (Math.random() * multi) + multi / 3;
            float val3 = (float) (Math.random() * multi) + multi / 3;
            float val4 = (float) (Math.random() * multi) + multi / 3;
            values1.add(new BarEntry(i, val1));
            values2.add(new BarEntry(i, val2));
            values3.add(new BarEntry(i, val3));
            values4.add(new BarEntry(i, val4));
        }
        BarDataSet set1 = new BarDataSet(values1, "Data Set1");
        set1.setColor(Color.parseColor("#6368D2"));
        set1.setDrawValues(false);

        BarDataSet set2 = new BarDataSet(values2, "Data Set2");
        set2.setColor(Color.parseColor("#8477E7"));
        set2.setDrawValues(false);

        BarDataSet set3 = new BarDataSet(values3, "Data Set3");
        set3.setColor(Color.parseColor("#BAB1F8"));
        set3.setDrawValues(false);

        BarDataSet set4 = new BarDataSet(values4, "Data Set4");
        set4.setColor(Color.parseColor("#FED857"));
        set4.setDrawValues(false);

        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        dataSets.add(set2);
        dataSets.add(set3);
        dataSets.add(set4);

        BarData data = new BarData(dataSets);
        data.setBarWidth(0.45f);
        mBarChart.setData(data);
        mBarChart.setFitBars(true);
        mBarChart.groupBars(0.0f, 0f, 0f);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sleepBack:
                this.finish();
                break;
            case R.id.sleepShare:
                // TODO: 分享
                break;
        }
    }
}
